<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="600px" class="speech-dialog">
    <div class="form-container">
      <el-form
        ref="formRef"
        :model="formData"
        :rules="formRules"
        label-width="100px"
        v-loading="formLoading"
        class="speech-form"
      >
        <!-- 基本信息区域 -->
        <div class="form-section">
          <div class="section-title">
            <Icon icon="ep:info-filled" class="section-icon" />
            <span>基本信息</span>
          </div>

          <el-form-item label="话术标题" prop="title" class="form-item">
            <el-input
              v-model="formData.title"
              placeholder="请输入话术标题"
              class="form-input"
              clearable
              maxlength="50"
              show-word-limit
            >
              <template #prefix>
                <Icon icon="ep:chat-line-square" class="input-icon" />
              </template>
            </el-input>
            <div class="input-tip">
              <Icon icon="ep:info-filled" class="tip-icon" />
              <span>建议使用简洁明了的标题，便于识别和管理</span>
            </div>
          </el-form-item>
        </div>

        <!-- 内容区域 -->
        <div class="form-section">
          <div class="section-title">
            <Icon icon="ep:document" class="section-icon" />
            <span>话术内容</span>
          </div>

          <el-form-item label="话术内容" prop="content" class="form-item">
            <el-input
              v-model="formData.content"
              placeholder="请输入话术内容\n支持多行文本，可以包含完整的对话脚本"
              type="textarea"
              :rows="8"
              class="form-textarea"
              show-word-limit
              maxlength="2000"
            ></el-input>
            <div class="content-stats">
              <div class="stats-item">
                <Icon icon="ep:document" class="stats-icon" />
                <span>字数：{{ formData.content?.length || 0 }}</span>
              </div>
              <div class="stats-item">
                <Icon icon="ep:clock" class="stats-icon" />
                <span>预计阅读：{{ Math.ceil((formData.content?.length || 0) / 300) }} 分钟</span>
              </div>
            </div>
            <div class="input-tip">
              <Icon icon="ep:warning" class="tip-icon" />
              <div class="tip-content">
                <p>• 建议包含完整的对话流程</p>
                <p>• 可以添加常见问题的应答话术</p>
                <p>• 注意语言的专业性和友好性</p>
              </div>
            </div>
          </el-form-item>
        </div>
      </el-form>
    </div>

    <template #footer>
      <div class="dialog-footer">
        <el-button class="cancel-btn" @click="dialogVisible = false">
          <Icon icon="ep:close" class="btn-icon" />
          取消
        </el-button>
        <el-button
          type="primary"
          class="submit-btn"
          :disabled="formLoading"
          :loading="formLoading"
          @click="submitForm"
        >
          <Icon icon="ep:check" class="btn-icon" v-if="!formLoading" />
          {{ formLoading ? '提交中...' : '确定' }}
        </el-button>
      </div>
    </template>
  </Dialog>
</template>

<script setup lang="ts">
import * as SpeechApi from '@/api/call/speech'

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  title: undefined,
  content: undefined
})
const formRules = reactive({
  title: [
    {
      required: true,
      message: '话术标题不能为空',
      trigger: 'blur'
    }
  ],
  content: [
    {
      required: true,
      message: '话术内容不能为空',
      trigger: 'blur'
    }
  ]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await SpeechApi.getSpeech(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as SpeechApi.SpeechVO
    if (formType.value === 'create') {
      await SpeechApi.createSpeech(data)
      message.success(t('common.createSuccess'))
    } else {
      await SpeechApi.updateSpeech(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    title: undefined,
    content: undefined
  }
  formRef.value?.resetFields()
}
</script>

<style lang="scss" scoped>
// 对话框样式
.speech-dialog {
  :deep(.el-dialog) {
    border-radius: 16px;
    overflow: hidden;

    .el-dialog__header {
      background: linear-gradient(135deg, #97a5e2 0%, #7db5db 100%);
      color: white;
      padding: 20px 24px;

      .el-dialog__title {
        color: white;
        font-size: 18px;
        font-weight: 600;
      }

      .el-dialog__headerbtn {
        .el-dialog__close {
          color: rgba(255, 255, 255, 0.8);
          font-size: 18px;

          &:hover {
            color: white;
          }
        }
      }
    }

    .el-dialog__body {
      padding: 0;
    }

    .el-dialog__footer {
      padding: 0;
      border-top: 1px solid #f1f5f9;
    }
  }
}

// 表单容器
.form-container {
  padding: 24px;
  max-height: 70vh;
  overflow-y: auto;
}

.speech-form {
  .form-section {
    margin-bottom: 32px;

    &:last-child {
      margin-bottom: 0;
    }
  }
}

// 区域标题
.section-title {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 20px;
  padding-bottom: 12px;
  border-bottom: 2px solid #f1f5f9;

  .section-icon {
    color: #667eea;
    font-size: 18px;
  }

  span {
    font-size: 16px;
    font-weight: 600;
    color: #374151;
  }
}

// 表单项
.form-item {
  margin-bottom: 24px;

  :deep(.el-form-item__label) {
    font-weight: 500;
    color: #374151;
    line-height: 1.6;
  }

  :deep(.el-form-item__content) {
    line-height: normal;
  }
}

// 输入框样式
.form-input {
  :deep(.el-input__wrapper) {
    border-radius: 8px;
    border: 2px solid #e5e7eb;
    transition: all 0.3s ease;

    &:hover {
      border-color: #d1d5db;
    }

    &.is-focus {
      border-color: #667eea;
      box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
    }
  }

  .input-icon {
    color: #9ca3af;
    font-size: 16px;
  }
}

// 文本域样式
.form-textarea {
  :deep(.el-textarea__inner) {
    border-radius: 8px;
    border: 2px solid #e5e7eb;
    transition: all 0.3s ease;
    font-family: inherit;
    resize: vertical;
    line-height: 1.6;

    &:hover {
      border-color: #d1d5db;
    }

    &:focus {
      border-color: #667eea;
      box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
    }
  }
}

// 内容统计
.content-stats {
  display: flex;
  gap: 20px;
  margin-top: 8px;
  padding: 12px;
  background: #f8fafc;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
}

.stats-item {
  display: flex;
  align-items: center;
  gap: 6px;

  .stats-icon {
    color: #667eea;
    font-size: 14px;
  }

  span {
    font-size: 13px;
    color: #475569;
    font-weight: 500;
  }
}

// 输入提示
.input-tip {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  margin-top: 8px;
  padding: 12px;
  background: #f0f9ff;
  border: 1px solid #e0f2fe;
  border-radius: 8px;

  .tip-icon {
    color: #0ea5e9;
    font-size: 16px;
    margin-top: 2px;
    flex-shrink: 0;
  }

  span {
    font-size: 13px;
    color: #0369a1;
    line-height: 1.4;
  }

  .tip-content {
    p {
      margin: 0 0 4px 0;
      font-size: 13px;
      color: #0369a1;
      line-height: 1.4;

      &:last-child {
        margin-bottom: 0;
      }
    }
  }
}

// 警告提示样式
.input-tip:has(.tip-icon[icon='ep:warning']) {
  background: #fef3c7;
  border-color: #fbbf24;

  .tip-icon {
    color: #f59e0b;
  }

  .tip-content p {
    color: #92400e;
  }
}

// 对话框底部
.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  padding: 20px 24px;
  background: #f8fafc;
}

.cancel-btn {
  padding: 10px 20px;
  border-radius: 8px;
  border: 2px solid #e5e7eb;
  background: white;
  color: #6b7280;
  font-weight: 500;
  transition: all 0.3s ease;

  .btn-icon {
    margin-right: 6px;
    font-size: 14px;
  }

  &:hover {
    border-color: #d1d5db;
    color: #374151;
  }
}

.submit-btn {
  padding: 10px 24px;
  border-radius: 8px;
  background: linear-gradient(135deg, #97a5e2 0%, #7db5db 100%);
  border: none;
  color: white;
  font-weight: 500;
  transition: all 0.3s ease;

  .btn-icon {
    margin-right: 6px;
    font-size: 14px;
  }

  &:hover:not(:disabled) {
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
  }

  &:disabled {
    opacity: 0.7;
    cursor: not-allowed;
  }
}

// H5 响应式设计
@media (max-width: 768px) {
  .speech-dialog {
    :deep(.el-dialog) {
      width: 95% !important;
      margin: 20px auto;

      .el-dialog__header {
        padding: 16px 20px;

        .el-dialog__title {
          font-size: 16px;
        }
      }
    }
  }

  .form-container {
    padding: 20px;
    max-height: 60vh;
  }

  .speech-form {
    :deep(.el-form-item__label) {
      font-size: 14px;
    }
  }

  .section-title {
    span {
      font-size: 15px;
    }
  }

  .content-stats {
    flex-direction: column;
    gap: 8px;
  }

  .dialog-footer {
    padding: 16px 20px;

    .cancel-btn,
    .submit-btn {
      padding: 12px 16px;
      font-size: 14px;
    }
  }
}

@media (max-width: 480px) {
  .speech-dialog {
    :deep(.el-dialog) {
      width: 100% !important;
      margin: 0;
      height: 100vh;
      border-radius: 0;
    }
  }

  .form-container {
    padding: 16px;
    max-height: calc(100vh - 140px);
  }

  .form-section {
    margin-bottom: 24px;
  }

  .form-item {
    margin-bottom: 20px;
  }

  .form-textarea {
    :deep(.el-textarea__inner) {
      min-height: 120px;
    }
  }

  .dialog-footer {
    flex-direction: column;
    gap: 8px;

    .cancel-btn,
    .submit-btn {
      width: 100%;
      justify-content: center;
    }
  }
}

// 深色模式支持
@media (prefers-color-scheme: dark) {
  .speech-dialog {
    :deep(.el-dialog) {
      background: #1e293b;

      .el-dialog__body {
        background: #1e293b;
      }
    }
  }

  .form-container {
    background: #1e293b;
  }

  .section-title {
    border-color: #334155;

    span {
      color: #f1f5f9;
    }
  }

  .form-item {
    :deep(.el-form-item__label) {
      color: #e2e8f0;
    }
  }

  .content-stats {
    background: #334155;
    border-color: #475569;

    .stats-item span {
      color: #e2e8f0;
    }
  }

  .input-tip {
    background: #1e3a8a;
    border-color: #1e40af;

    span,
    .tip-content p {
      color: #93c5fd;
    }
  }

  .dialog-footer {
    background: #0f172a;
    border-color: #334155;
  }
}
</style>
